package com.shiwaixiangcun.customer.module.heath

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.shiwaixiangcun.customer.ConfigSps
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.tencent.smtt.sdk.*
import kotlinx.android.synthetic.main.activity_evaluation_detail.*
import kotlinx.android.synthetic.main.top_bar_white.*
import java.util.*

class EvaluationDetailActivity : BaseActivity(), View.OnClickListener {


    private var evaluationId: Long? = 0
    private var requestUrl: String? = null
    private var strToken: String? = null
    private var type: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_evaluation_detail)
        initBundleData()

        initViewAndEvent()
        initData()
    }


    private fun initData() {


    }

    private fun initViewAndEvent() {

        tv_page_name.text = ""
        back_left.setOnClickListener(this)

        strToken = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_TOKEN, "") as String

        requestUrl = "${GlobalApi.EVALUATION_DETAIL}?access_token=$strToken&id=$evaluationId&assessmentType=$type"

        removeCookie(mContext)
        webView.clearCache(true)

        //获取本地user_agent;
        val userAgentString = webView.settings.userAgentString
        //设置user_agent(以asyncHttprequest为例)
//        client.setUserAgent(defaultUserAgent);

        val header = HashMap<String, String>()
        header.put("user-agent", userAgentString)
        webView.loadUrl(requestUrl, header)
        webView.webChromeClient = MyWebChromeViewClient()
        webView.webViewClient = MyWebViewClient()

        Log.e(BUG_TAG, requestUrl)


    }

    private fun initBundleData() {
        type = intent.extras.getString("evaluationType")
        evaluationId = intent.extras.getLong("evaluationId")


    }

    /**
     * 清除Cookie
     *
     * @param context
     */
    fun removeCookie(context: Context) {
        CookieSyncManager.createInstance(context)
        val cookieManager = CookieManager.getInstance()
        cookieManager.removeAllCookie()
        CookieSyncManager.getInstance().sync()
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.back_left -> finish()
            else -> {
            }
        }


    }


    override fun onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
            webView.clearHistory()

            (webView.parent as ViewGroup).removeView(webView)
            webView.destroy()

        }
        super.onDestroy()
    }


    override fun getResources(): Resources {
        val res = super.getResources()
        val config = Configuration()
        config.setToDefaults()
        res.updateConfiguration(config, res.displayMetrics)
        return res
    }

    private inner class MyWebChromeViewClient : WebChromeClient() {


        override fun onProgressChanged(view: WebView, newProgress: Int) {
            if (newProgress == 100) {
                myProgressBar.visibility = View.GONE
            } else {
                if (View.INVISIBLE == myProgressBar.visibility) {
                    myProgressBar.visibility = View.VISIBLE
                }
                myProgressBar.progress = newProgress
            }
            super.onProgressChanged(view, newProgress)
        }


    }

    /**
     * 处理通知 ，请求事件
     */
    private inner class MyWebViewClient : WebViewClient() {
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            val strTitle = view?.title
            if (strTitle != null) {
                tv_page_name.text = strTitle
            }

        }

        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }
    }

}
